/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import business.XYRecord;
import data.TrainingTable;
import java.awt.Color;
import java.util.Date;
import java.util.List;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author stana
 */
public class Chart
{
    private static final long serialVersionUID = 1L;
    private XYDataset dataset;
    private TrainingTable trainingTable;
    //private Date date;
    private JFreeChart chart;
    List<XYRecord> list;
    String chartName,xAxisName,yAxisName,serieName;
    
    public Chart(String chartName, String xAxisName, String yAxisName, String serieName)
    {
        this.chartName = chartName;
        this.xAxisName = xAxisName;
        this.yAxisName = yAxisName;
        this.serieName = serieName;
    }
    
    public JPanel getPanel(List<XYRecord> list)
    {        
        this.list=list;
        createXYDataset();
        createChart();
        return new ChartPanel(chart);
    }
    
    private void createXYDataset()
    {

        
        final XYSeries series1 = new XYSeries(serieName);
        int i = 0;
        for (XYRecord r : list)
        {
            series1.add(r.x, r.y);
        }
        dataset = new XYSeriesCollection(series1);
    }

    /** * Creates a chart */
    private JFreeChart createChart()
    {
        chart = ChartFactory.createXYLineChart(
                chartName, // chart title
                xAxisName, // x axis label
                yAxisName, // y axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // include legend
                true, // tooltips
                false // urls
                );
        // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.LIGHT_GRAY);
//        final StandardLegend legend = (StandardLegend) chart.getLegend();
        //      legend.setDisplaySeriesShapes(true);

        // get a reference to the plot for further customisation...
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.white);
        //    plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.DARK_GRAY);
        plot.setRangeGridlinePaint(Color.DARK_GRAY);

        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(0, true);
        renderer.setSeriesShapesVisible(0, false);
        renderer.setSeriesPaint(0, Color.BLUE);
        plot.setRenderer(renderer);
        chart.setBorderStroke(null);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // OPTIONAL CUSTOMISATION COMPLETED.

        return chart;

    }
}
